package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.FindUserAdapter;
import com.dingwei.superiordiary.bean.FIndUserBean;
import com.dingwei.superiordiary.mvp.presenter.SearchUserPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.KeyboardUtils;
import com.lihao.baselibrary.weight.TitleBar;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author 南柯一梦丶
 * Created time 2018/11/2.
 * Personal signature 余生还长 何必慌张
 * Page description :
 * .::::.
 * .::::::::.
 * :::::::::::
 * ..:::::::::::'
 * '::::::::::::'
 * .::::::::::
 * '::::::::::::::..
 * ..::::::::::::.
 * ``::::::::::::::::
 * ::::``:::::::::'        .:::.
 * ::::'   ':::::'       .::::::::.
 * .::::'      ::::     .:::::::'::::.
 * .:::'       :::::  .:::::::::' ':::::.
 * .::'        :::::.:::::::::'      ':::::.
 * .::'         ::::::::::::::'         ``::::.
 * ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 * '.:::::'                    ':'````..
 */
public class SearchUserActivity extends BaseActivity implements Contract.SearchUserView {

    SearchUserPresenter searchUserPresenter = new SearchUserPresenter(this);

    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;



    FindUserAdapter adapter;
    @Override
    protected int getContentViewId() {
        return R.layout.activity_edit_search_user;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.line), 1));
        adapter = new FindUserAdapter(this);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        etSearch.setOnEditorActionListener(listener);

        adapter.attentionInterfaceListener(new FindUserAdapter.attentionInterFace() {
            @Override
            public void attention(int position) {
                FIndUserBean.DataBean data = adapter.getItem(position);
                int action = 0;
                if (data.attentioned == 0) {
                    action = 1;
                } else {
                    action = 0;
                }
                searchUserPresenter.attention(data.id, position, action);
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int i) {
                FIndUserBean.DataBean bean = adapter.getItem(i);
                intent = new Intent(BaseActivity.context,LookUserCenterActivity.class);
                intent.putExtra("user_id",bean.id);
                startActivity(intent);
            }
        });
    }

    /**
     * 搜索框监听
     */
    TextView.OnEditorActionListener listener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                KeyboardUtils.hideSoftInput(BaseActivity.context);
                String keyword = etSearch.getText().toString().trim();
                if (!TextUtils.isEmpty(keyword)) {
                    searchUserPresenter.search(keyword);
                }
            }
            return false;
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void setData(FIndUserBean bean) {
        adapter.clear();
        adapter.addAll(bean.data);
    }

    @Override
    public void attentionSuccess(int position) {
        FIndUserBean.DataBean listBean = adapter.getItem(position);
        if (listBean.attentioned == 0) {
            listBean.attentioned = 1;
        } else {
            listBean.attentioned = 0;
        }
        adapter.notifyDataSetChanged();
    }
}
